#!/bin/bash
# 如果没有 ~/tools 目录，则先创建
TC_DIR=~/tools
[ -e $TC_DIR ] || mkdir $TC_DIR

function do_curl()
{
    URL=$1
    TAR=$2
    echo "curl $URL/$TAR ..."
    if [ ! -e $TAR ]; then
        curl $URL/$TAR > $TAR
    fi
}

function do_unzip()
{
    DIR=$1
    TAR=$2
    echo "unzip $TAR to $DIR ..."
    tar -C $DIR -xvf $TAR
}

# 下载并解压 gn 压缩包
GN_TARBALL=gn-linux-x86-1717.tar.gz
GN_URL_PREFIX=https://repo.huaweicloud.com/harmonyos/compiler/gn/1717/linux
do_curl $GN_URL_PREFIX $GN_TARBALL

# 解压gn压缩包
do_unzip $TC_DIR $GN_TARBALL
echo 'export PATH=$PATH:~/tools' | tee -a ~/.bashrc

# 下载 ninja 压缩包
NINJA_TARBALL=ninja.1.9.0.tar
NINJA_URL_PREFIX=https://repo.huaweicloud.com/harmonyos/compiler/ninja/1.9.0/linux
do_curl $NINJA_URL_PREFIX $NINJA_TARBALL

# 解压gninja压缩包
do_unzip $TC_DIR $NINJA_TARBALL
echo 'export PATH=$PATH:~/tools/ninja' | tee -a ~/.bashrc

# 下载clang 9.0 压缩包
CLANG_TARBALL=llvm-linux-9.0.0-36191.tar
CLANG_URL_PREFIX=https://repo.huaweicloud.com/harmonyos/compiler/clang/9.0.0-36191/linux
do_curl $CLANG_URL_PREFIX $CLANG_TARBALL

# 解压clang压缩包
do_unzip $TC_DIR $CLANG_TARBALL
echo 'export PATH=$PATH:~/tools/llvm/bin' | tee -a ~/.bashrc

# 下载 gcc-arm-none-eabi-10-2020-q4-major 压缩包
GCC_TARBALL=gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2
GCC_URL_PREFIX=https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-rm/10-2020q4
do_curl $GCC_URL_PREFIX $GCC_TARBALL

# 解压gcc压缩包
do_unzip $TC_DIR $GCC_TARBALL
echo 'export PATH=$PATH:~/tools/gcc-arm-none-eabi-10-2020-q4-major/bin' | tee -a ~/.bashrc

source ~/.bashrc # 生效环境变量
